package com.spzx.user.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.user.service.ISmsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * projectName: spzx-parent
 *
 * @author: 王宝贵
 * time: 2024/10/12 16:36 周六
 * description: 短信接口
 */
@Tag(name = "短信接口")
@RestController
@RequestMapping("/sms")
@Slf4j
public class SmsController  extends BaseController {


    @Autowired
    private ISmsService smsService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Operation(summary = "发送验证码")
    @GetMapping(value = "sendCode/{phone}")
    public AjaxResult sendCode(@PathVariable() String phone) throws ServletException {
        //随机四位数字验证码
        String code = new DecimalFormat("0000").format(new Random().nextInt(10000));
        //放到redis中
        redisTemplate.opsForValue().set("phone:code:" +phone,code, 5, TimeUnit.MINUTES);
        log.info("验证码为："+code);

        HashMap<String ,Object> hashMap =new HashMap<>();
        hashMap.put("code",code);
        smsService.sendCode(phone,"",hashMap);
        return  success();
    }

}